// pages/mine/mine.js
var myId=1;
var ID;
var masterId;
var wxNum;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      questions:"",
      NoshowQue: 'display',
      showQue: 'none',
      monthScore:"",
      sumScore:"",
      question_class:[
        {
          text:'回答的普通问题',
          parameter:'openId',
          index:1
        },
        {
          text:'回答的专属问题',
          parameter:'openid',
          index:2
        }
      ],
      border_left: 0
  },
  choose_class:function(e){
    var index=e.currentTarget.id;
    var datas=this.data.question_class;
    var Type=datas[index].parameter;
    var move=this.data.border_width*index;
   
    this.setData(
      {
        border_left:move
      }
    );
    switch(Type){
      case 'openid':{
        this.questions_special();
      };break;
      case 'openId':{
        this.questions();
      };break;
    };
  },
  /**
   * 生命周期函数--监听页面加载
   */
  toquestion:function(e){
    ID=e.currentTarget.id;
    ID=this.data.questions[ID].id;
    wx.navigateTo({
      url: '../answered/answered?id='+ID,
    })
  },
  special:function(){
    wx.navigateTo({
      url: '../priority/priority',
    })
  },
  addObject: function (a, b) {
    a = JSON.stringify(a);
    b = JSON.stringify(b);
    a = a.concat('。');
    a = a.replace('}。', "");
    b = b.replace('{', ",");
    a = a.concat(b);
    a = JSON.parse(a);
    return a;
  },
  questions:function(){
    var that=this;
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByAnswerMaster",
      data:{
        openId:masterId
      },
      method:'GET',
      success:function(res){
        console.log(res.data,'专家回答的问题')
        /*for(var i=0;i<res.data.length;i++)
        if(res.data[i].questionClass.length<3){
        res.data[i].questionClass='特问';
        var a={"size":"26","color":"black"};
        res.data[i]=that.addObject(res.data[i],a);
        }*/
        that.setData(
          {
            questions:res.data,
            NoshowQue: 'none',
            showQue: 'display'
          }
        );
      },
      
    })
  },
  questions_special: function () {
    var that = this;
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findTewenQuestion",
      data: {
        openid: masterId
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data, '专家回答的问题')
        /*for(var i=0;i<res.data.length;i++)
        if(res.data[i].questionClass.length<3){
        res.data[i].questionClass='特问';
        var a={"size":"26","color":"black"};
        res.data[i]=that.addObject(res.data[i],a);
        }*/
        that.setData(
          {
            questions: res.data,
            NoshowQue: 'none',
            showQue: 'display'
          }
        );
      },

    })
  },
  tologin: function () {
  
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLoginMaster',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log("return" + res.data);
              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });
              that.CheckLogin();
              

            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  Login: function () {
    var that = this;

    wx.checkSession({
      success: function () {
        wx.getStorage({
          key: 'login_uuId',
          success: function (res) {

           
         
            wx.request({
              url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
              data: {
                login_uuId: res.data
              },
              success: function (res) {
                if (res.data === 'error') {
                 
                  that.tologin();

                }
                else {
                    
                    that.CheckLogin();
                 
                }
              },

            })

          },
        })

      },
      fail: function () {
        console.log("fail")
        that.tologin();
      },

    })


    //

  },
  score:function(){
    var that=this;

    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/sumMasterScore',
      data:{
        openid:masterId
      },
      success:function(res){
        that.setData({
          monthScore:res.data.monthScore,
          sumScore:res.data.sumScore,
        });
        console.log(res.data)
      },
    })
    
  },
  CheckLogin:function(){
    var that=this;
    wx.getStorage({
      key: 'login_uuId',
      success: function(res) {
        console.log('加载问题和积分')
        masterId=res.data;
        that.questions();
        that.score();
      },
      fail:function(){
       that.tologin();
      },
    })
  },
  view:function(){
    var num=this.data.question_class.length;
    num=100/num;
    this.setData(
      {
        border_width:num,
        border_left:0
      }
    );
  },
  onLoad: function (options) {
      this.Login();
      this.view();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.CheckLogin();
    // this.setData(
    //   {
    //     border_left: 0
    //   }
    // );
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})